--[[
 -- *名称:SoftBombTypeAnalyzer
 -- *描述:软炸弹
 -- *版权:Copyright © 2016-2018 深圳市三只小熊科技有限公司 版权所有
 -- *作者:Administrator
 -- *创建日期:2017-11-10
 -- *修改日期:2017-11-10
 ]]
local BobmSuitAnalyzer = require(basePath.."suit/bomb/bombtypeanalyzer");
local SoftBombTypeAnalyzer = pokercore.class(BobmSuitAnalyzer,false);

local TUPLE_SIZE = 3 ;
SoftBombTypeAnalyzer.TUPLE_SIZE = 3 ;

function SoftBombTypeAnalyzer:getSuitType()
	return DdzCardTypes.SOFT_BOMB;
end


--@Override
function SoftBombTypeAnalyzer:doCheckRule( cards, term, result )
	if result:getCardNum() == TUPLE_SIZE then 
		if (result:getLaziCardNum() == 0 and result:getChangedLaziCardNum() == 0)
		or result:isAllCardLazi() then -- 没有癞子，都是癞子
			--print("SoftBombTypeAnalyzer:doCheckRule 1");
			return false;
		else --癞子的情况
			if (result:getNoneLaziValueCounter():hasJoker()) then -- // 非赖中含有大小王
					--print("SoftBombTypeAnalyzer:doCheckRule 2");
					return false;
			end
			local cardList = cards:subList(1, result:getNoneCardNum()); --// 取出非癞子部分的牌
			----dump(cardList,"SoftBombTypeAnalyzer:doCheckRule cardList ");
			local firstCardValue = cardList[1]:getValue();
			for i=1,#cardList do
				if (cardList[i]:getValue() ~= firstCardValue) then
					--print("SoftBombTypeAnalyzer:doCheckRule 3");
					return false;
				end
			end
			return true;
		end
	else
		--print("SoftBombTypeAnalyzer:doCheckRule 4");
		return false;
	end
end

--@Override
function SoftBombTypeAnalyzer:doAnalysizeLaizi( cards, term, onlyMaxValue )
	local cardsResult = {};
	local result = cards:getResult();
	local laiziCardList = cards:subList(result:getCardNum() - result:getLaziCardNum() + 1, result:getCardNum()) ;
	local value = result:getNoneLaziValueCounter():getAllValues()[1];
	for _,card in ipairs(laiziCardList) do -- // 设置number
		card:changeCard(value);
	end
	local cardsCopy = cards:clone();	
	self:analysize(cardsCopy,term);
	table.insert(cardsResult,cardsCopy);
	return cardsResult;
end

--@Override
function SoftBombTypeAnalyzer:containsPossibleCards(handCards,term) 
	local result = handCards:getResult();
	if (result:getCardNum() >= TUPLE_SIZE) then
		local noneLaiziValues = result:getNoneLaziValueCounter():getAllValues();
		local maxValueNum= 0;
		for _,valueTemp in ipairs(noneLaiziValues) do
			local valueTempNum = result:getNoneLaziValueCounter():getValueCount(valueTemp);
			if (valueTempNum > maxValueNum) then
				maxValueNum = valueTempNum;
			end
		end
		--// 软炸弹数量足够
		return result:getLaziCardNum() > 0 and result:getLaziCardNum() + maxValueNum >= TUPLE_SIZE;
	else 
		return false;
	end
end
	
--/** 最多使用癞子的数量 */
local MAX_USE_LAIZI_CARD_NUM = 2;
	
--@Override
function SoftBombTypeAnalyzer:doListPossibleCards(handCards,term)
	local cardsResult = {};
	--// 非癞子部分
	local result = handCards:getResult();
	local noneLaiziCardList = handCards:subList(1, result:getNoneCardNum());
	local noneLaiziCards = DdzTypeUtil.createCardsCopy(noneLaiziCardList);
	noneLaiziCards:setResult(handCards:getResult():clone());
	--// 癞子部分
 	local laiziCardList = handCards:subList(result:getNoneCardNum()+1, result:getCardNum());
	local laiziCards = DdzTypeUtil.createCardsCopy(laiziCardList);
	laiziCards:setResult(handCards:getResult():clone());

	local laiziCardNum = result:getLaziCardNum();
	local maxUseLaiziCardNum = MAX_USE_LAIZI_CARD_NUM < laiziCardNum and MAX_USE_LAIZI_CARD_NUM or laiziCardNum;
	for useLaiziCardNum = 1,maxUseLaiziCardNum do
		local useLaiziCardList = DdzTypeUtil.cloneSublistedCards(laiziCards, 1, useLaiziCardNum);--// 补癞子
		--dump(useLaiziCardList,"useLaiziCardList->");
		local needCardNum = TUPLE_SIZE - useLaiziCardNum;
		local cardsList = {};
		if (needCardNum == 1) then
			table.addAll(cardsList,DdzTypeUtil.selectSingleCards(noneLaiziCards, false));
		else 
			table.addAll(cardsList,DdzTypeUtil.selectTupleCountCards(noneLaiziCards, needCardNum));
		end
		--dump(cardsList,"cardsList->");
		for _,usecards in ipairs(cardsList) do
			local card = usecards:get(1);
			for _,laiziCard in ipairs(useLaiziCardList) do
				laiziCard:changeCard(card:getValue());
			end
			local cardsCopy = DdzTypeUtil.cloneCardsAndAddCardList(usecards, useLaiziCardList);
			--dump(cardsCopy,"cardsCopy->");
			if (self:match(cardsCopy, term)) then
				self:analysize(cardsCopy, term);
				table.insert(cardsResult,cardsCopy:clone());
			end
		end
	end
	return cardsResult;
end


return SoftBombTypeAnalyzer;

